﻿// XmlVisualizer
// Copyright (C) 2009  Roman Betík - rbetik@gmail.com

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

using XmlVisualizer;

namespace XmlVisualizer.Utils {

    /// <summary>
    /// Helper class for string manipulations.
    /// </summary>
    public static class StringUtils {

        /// <summary>
        /// Gets the file extention for the specified filename.
        /// </summary>
        /// <param name="filename">file name to get the extension from.</param>
        /// <returns>file extension or null.</returns>
        public static String GetExtension(String filename) {
            return System.IO.Path.GetExtension(filename).ToLower();
        }

        /// <summary>
        /// Parses string with range definition.
        /// </summary>
        /// <param name="rangeString">String with range, e.g. 1-10,12-15,16</param>
        /// <returns>Return IEnumerable with ints representing individual numbers in desired range.</returns>
        public static IEnumerable<int> RangeParsing(String rangeString) {
            List<int> res = new List<int>();

            foreach (string s in rangeString.Split(',')) {
                //try and get the number
                int num;
                if (int.TryParse(s, out num))
                    res.Add(num);
                //yield return num;

                //otherwise we might have a range
                else {
                    //split on the range delimiter
                    string[] subs = s.Split('-');
                    int start, end;

                    //now see if we can parse a start and end
                    if (subs.Length > 1 &&
                        int.TryParse(subs[0], out start) &&
                        int.TryParse(subs[1], out end) &&
                        end > start)

                        //create a range between the two values
                        foreach (int i in Enumerable.Range(start, end - start + 1))
                            res.Add(i);
                            //yield return i;
                }
            }
            res.Sort();
            return res.Distinct();
        }
    }
}